<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TemplateController
 *
 * @author Poh Nean
 */
class TemplateController extends PsController
{
	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'users' => array('@'),
					)), parent::accessRules());
	}

	public function actionListPropertyPhotos($id = '', $term = '')
	{
		$response = array();
		try {
			$data = array();
			$data['albums'] = array();
			if ($id == '') {
				// list all
				$criteria = new CDbCriteria();
				$criteria->order = 't.create_time DESC';
				$criteria->with = array('property', 'property.propertyPhotos');
				$criteria->compare('t.user_id', Yii::app()->user->id);
				$criteria->compare('t.user_id', Yii::app()->user->id);
				$criteria->addCondition('EXISTS (SELECT * FROM `property_photo` p WHERE p.property_id = property.id)');

				$propertyCases = PropertyCase::model()->ofCurrentUser()->notRemoved()->findAll($criteria);

				if (!empty($propertyCases)) {
					foreach ($propertyCases as $propertyCase) {
						$temp = array();

						$nameParts = explode("\n", $propertyCase->getTitle(120, false));
						$temp['name'] = $nameParts[0];

						if (!empty($propertyCase->property->propertyPhotos)) {
							$temp['photos'] = array();
							foreach ($propertyCase->property->propertyPhotos as $propertyPhoto) {
								$attributes = $propertyPhoto->attributes;
								$attributes['thumbnail'] = $propertyPhoto->getThumbnailUrl();
								$attributes['url'] = $propertyPhoto->getUrl();

								unset($attributes['user_file_id']);

								$temp['photos'][] = $attributes;
							}
						}

						$data['albums'][] = $temp;
					}
				}
			} else {
				// list one
				$propertyCase = $this->loadModel($id);
			}

			$response['result'] = 'success';
			$response['data'] = $data;
		} catch (Exception $e) {
			$response['result'] = 'failure';
		}

		print(CJSON::encode($response));
	}

	public function actionListFloorplanPhotos($term = '')
	{
		$response = array();
		try {
			$data = array();
			$data['albums'] = array();

			// list all
			$criteria = new CDbCriteria();
			$criteria->order = 't.id ASC';
//			$criteria->with = array('userFile');
//			$criteria->compare('t.type', 'Floorplan');
//			$floorPlans = SpaceImage::model()->ofCurrentAgency()->findAll($criteria);
			$projects = PropertyProject::model()->ofCurrentAgency()->findAll($criteria);

			if (!empty($projects)) {
				foreach ($projects as $project) {
					$spaces = $project->spaces;
					foreach ($spaces as $space) {
						$blueprints = $space->floorplans;

						foreach ($blueprints as $blueprint) {
							$temp = array();
							$temp['name'] = $blueprint->name;

							if (!empty($blueprint)) {
								$temp['photos'] = array();
								$temp['photos'][] = array(
									'thumbnail' => $blueprint->thumbnail,
									'url' => $blueprint->url,
								);
							}

							$data['albums'][] = $temp;
						}
					}
				}
			}

//			if (!empty($floorPlans)) {
//				foreach ($floorPlans as $floorPlan) {
//					$temp = array();
//
//					$temp['name'] = $floorPlan->name;
//
//					if (!empty($floorPlan)) {
//						$temp['photos'] = array();
//						$temp['photos'][] = array(
//							'thumbnail' => $floorPlan->thumbnail,
//							'url' => $floorPlan->url,
//						);
//					}
//
//					$data['albums'][] = $temp;
//				}
//			}

			$response['result'] = 'success';
			$response['data'] = $data;
		} catch (Exception $e) {
			$response['result'] = 'failure';
			$response['e'] = $e->getMessage();
		}

		print(CJSON::encode($response));
	}

}

?>
